package com.zdth.wxpush.util;

import cn.gjing.http.HttpClient;
import cn.gjing.http.HttpMethod;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.zdth.wxpush.model.Constellation;
import com.zdth.wxpush.model.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.net.URL;

/**
 * <a href="https://xiaobai.klizi.cn/">小白api</a>
 * @Author Z-7
 * @Date 2022/8/29
 */
@Slf4j
@Component
public class ApiUtil {
    @Value("${file.picPath}")
    private String picPath;

    /**
     * 上海地区
     * @param index
     * @param area
     * @return
     */
    public R getWeather(Integer index,String area) {
        R r = new R();
        String s = HttpClient.builder("http://ovooa.com/API/santq/api.php?msg=%E4%B8%8A%E6%B5%B7", HttpMethod.GET, String.class).execute().get();
        JSONObject entries = JSONUtil.parseObj(s);
        JSONObject data = entries.getJSONObject("data");
        // 小贴士
        String tips = data.getStr("Tips");
        r.setTips(tips);
        // 城市
        String city = data.getStr("city");
        r.setProvince(city);
        // 解析数据
        JSONArray weather = data.getJSONArray("data");
        JSONObject jsonObject =  null;
        if (index == 7) {
            jsonObject = weather.getJSONObject(0);
        } else if (index == 8) {
            jsonObject = weather.getJSONObject(1);
        }
        r.setInfo(jsonObject.getStr("weather"));
        r.setMaxTemp(jsonObject.getStr("high_temperature"));
        r.setMinTemp(jsonObject.getStr("low_temperature"));
        r.setDirect(jsonObject.getStr("wind_direction"));
        r.setPower(jsonObject.getStr("wind_speed"));
        r.setCity(area);
        return r;
    }

    /**
     * 每日热搜
     * @return
     */
    public R getTopSearch() {
        R r = new R();
        String s = HttpClient.builder("https://xiaobai.klizi.cn/API/other/toutiao.php", HttpMethod.GET, String.class).execute().get();
        String[] split = s.split("\\d+:");
        StringBuffer sb = new StringBuffer();
        for (int i = 1; i <= split.length - 1; i++) {
            sb.append(i + ":" + split[i] + "\\\n");
        }
        String replace = sb.toString().replace('\\', ' ');
        r.setTopSearch(replace);
        return r;
    }

    /**
     * 获取彩虹屁
     * @return
     */
    public String getLinKing() {
        return HttpClient.builder("https://xiaobai.klizi.cn/API/other/chp.php", HttpMethod.GET, String.class).execute().get();
    }

    /**
     * 获取买家秀
     * @return
     */
    public File getPic() {
        log.info("===获取买家秀开始===");
        String s = HttpClient.builder("https://api.vvhan.com/api/tao?type=json", HttpMethod.GET, String.class).execute().get();
        JSONObject entries = JSONUtil.parseObj(s);
        File pic = uriToFile(entries.getStr("pic"));
        log.info("===获取买家秀结束===，图片地址：{}",pic.getPath());
        return pic;
    }

    public String getPicUrl() {
        log.info("===获取买家秀开始===");
        String s = HttpClient.builder("https://api.vvhan.com/api/tao?type=json", HttpMethod.GET, String.class).execute().get();
        JSONObject entries = JSONUtil.parseObj(s);
        return entries.getStr("pic");
    }

    public File getMoYu() {
        log.info("===获取摸鱼日志开始===");
        String s = HttpClient.builder("https://api.vvhan.com/api/moyu?type=json", HttpMethod.GET, String.class).execute().get();
        JSONObject entries = JSONUtil.parseObj(s);
        log.info("===获取摸鱼日志结束===");
        return uriToFile(entries.getStr("url"));
    }

    /**
     * 舔狗日记
     * @return
     */
   public String getDog() {
       return HttpClient.builder("http://ovooa.com/API/tgrj/api.php", HttpMethod.GET, String.class).execute().get();
   }

    /**
     * 获取星座当日解析
     * @param name 星座名称 对应十二星座的英文 sagittarius 射手座 leo 狮子座
     * @return
     */
   public Constellation getConstellation(String name) {
       Constellation constellation = new Constellation();
       String s = HttpClient.builder("https://api.vvhan.com/api/horoscope?type=" + name + "&time=today", HttpMethod.GET, String.class).execute().get();
       JSONObject entries = JSONUtil.parseObj(s);
       JSONObject data = entries.getJSONObject("data");
       // 标题 幸运数字、颜色 短评 速配
       constellation.setTitle(data.getStr("title"));
       constellation.setLuckyNumber(data.getStr("luckynumber"));
       constellation.setLuckyColor(data.getStr("luckycolor"));
       constellation.setShortComment(data.getStr("shortcomment"));
       constellation.setLuckyConstellation(data.getStr("luckyconstellation"));

       // 指数
       JSONObject fortune = data.getJSONObject("fortune");
       constellation.setAllFortuneNum(fortune.getStr("all"));
       constellation.setLoveFortuneNum(fortune.getStr("love"));
       constellation.setWorkFortuneNum(fortune.getStr("work"));
       constellation.setMoneyFortuneNum(fortune.getStr("money"));

       // 运势
       JSONObject fortunetext = data.getJSONObject("fortunetext");
       constellation.setAllFortune(fortunetext.getStr("all"));
       constellation.setLoveFortune(fortunetext.getStr("love"));
       constellation.setWorkFortune(fortunetext.getStr("work"));
       constellation.setMoneyFortune(fortunetext.getStr("money"));
       constellation.setHealthFortune(fortunetext.getStr("health"));
       return constellation;
   }

    /**
     * uri -> file
     */
    public File uriToFile(String uri) {
        URL url = null;
        File file = null;
        try {
            file = new File(picPath + DateUtil.today() + ".png");
            url = new URL(uri);
            FileUtils.copyURLToFile(url,file);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return file;
    }

    /**
     * 其他地区
     * @param index
     * @param area
     * @return
     */
    public R getOtherWeather(Integer index,String area) {
        R r = new R();
        String s = HttpClient.builder("http://ovooa.com/API/santq/api.php?msg=%E4%B8%9C%E8%8E%9E", HttpMethod.GET, String.class).execute().get();
        JSONObject entries = JSONUtil.parseObj(s);
        JSONObject data = entries.getJSONObject("data");
        // 小贴士
        String tips = data.getStr("Tips");
        r.setTips(tips);
        // 城市
        String city = data.getStr("city");
        r.setCity(city);
        r.setProvince(area);
        // 解析数据
        JSONArray weather = data.getJSONArray("data");
        JSONObject jsonObject =  null;
        if (index == 7) {
            jsonObject = weather.getJSONObject(0);
        } else if (index == 8) {
            jsonObject = weather.getJSONObject(1);
        }
        r.setInfo(jsonObject.getStr("weather"));
        r.setMaxTemp(jsonObject.getStr("high_temperature"));
        r.setMinTemp(jsonObject.getStr("low_temperature"));
        r.setDirect(jsonObject.getStr("wind_direction"));
        r.setPower(jsonObject.getStr("wind_speed"));
        return r;
    }
}
